<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="设备id" prop="lotDeviceId">
        <el-input v-model="dataForm.lotDeviceId" placeholder="设备id"></el-input>
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceKind">
        <el-select v-model="dataForm.deviceKind" filterable placeholder="请选择">
          <el-option
            v-for="item in deviceKindData"
            :key="item.label"
            :label="item.label"
            :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="设备种类" prop="deviceKind">-->
      <!--        <el-input v-model="dataForm.deviceKind" placeholder="设备种类：顶膜、侧膜、遮阳帘、补光灯、风机等"></el-input>-->
      <!--      </el-form-item>-->
      <el-form-item label="设备名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="设备名称"></el-input>
      </el-form-item>
      <el-form-item label="用户" prop="userId">
        <!--        <el-input disabled v-model="dataForm.userId" placeholder="用户id"></el-input>-->
        <el-select
          v-model="dataForm.userId"
          filterable
          remote
          clearable
          @change="userChange"
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="searchUserByName"
          :loading="searchUserLoading">
          <el-option
            v-for="item in userPage.userDataList"
            :key="item.userId"
            :label="item.username"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="sim卡号" prop="simCode">
        <el-input v-model="dataForm.simCode" placeholder="卡号"></el-input>
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-input v-model="dataForm.province" placeholder="省"></el-input>
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input v-model="dataForm.city" placeholder="市"></el-input>
      </el-form-item>
      <el-form-item label="区" prop="area">
        <el-input v-model="dataForm.area" placeholder="区"></el-input>
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
      </el-form-item>
      <el-form-item label="维度" prop="latitude">
        <el-input v-model="dataForm.latitude" placeholder="维度"></el-input>
      </el-form-item>
      <!--    <el-form-item label="创建时间" prop="createTime">-->
      <!--      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>-->
      <!--    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        lotDeviceId: '',
        deviceType: '',
        deviceKind: '',
        name: '',
        userId: '',
        longitude: '',
        latitude: '',
        createTime: '',
        province: '',
        city: '',
        area: '',
        simCode: ''
      },
      dataRule: {
        lotDeviceId: [
          {required: true, message: '绑定的物联网设备唯一id不能为空', trigger: 'blur'}
        ],
        deviceType: [
          {required: true, message: '0控制类设备、1传感器类设备、2其他设备不能为空', trigger: 'blur'}
        ],
        deviceKind: [
          {required: true, message: '设备类型不能为空', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '设备名称不能为空', trigger: 'blur'}
        ],
        userId: [
          {type: 'number', min: 0, max: 9999999, required: true, message: '用户不能为空', trigger: 'blur'}
        ],
        longitude: [
          {required: false, message: '经度不能为空', trigger: 'blur'}
        ],
        latitude: [
          {required: false, message: '维度不能为空', trigger: 'blur'}
        ],
        createTime: [
          {required: false, message: '创建时间不能为空', trigger: 'blur'}
        ]
      },
      userPage: {
        dataListLoading: false,
        userDataList: [],
        limit: 10,
        page: 1,
        total: 0
      },
      userForm: {
        username: ''
      },
      loading: false,
      selectUserId: 0,
      searchUserLoading: false,
      deviceKindData: []
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/device/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            console.info(data)
            if (data && data.code === 0) {
              this.dataForm.lotDeviceId = data.data.lotDeviceId
              this.dataForm.deviceType = data.data.deviceType
              this.dataForm.deviceKind = data.data.deviceKind
              this.dataForm.name = data.data.name
              this.dataForm.userId = data.data.userId
              this.dataForm.longitude = data.data.longitude
              this.dataForm.latitude = data.data.latitude
              this.dataForm.createTime = data.data.createTime
              this.dataForm.simCode = data.data.simCode
              this.dataForm.province = data.data.province
              this.dataForm.city = data.data.city
              this.dataForm.area = data.data.area
            }
          })
        }
      })
      this.getAppUserList()
      this.getDeviceKind()
    },
    getDeviceKind () {
      this.$http({
        url: this.$http.adornUrl('/sys/sysdictdetail/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.deviceKindData = data.page.list
        } else {
          this.deviceKindData = []
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/device/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'lotDeviceId': this.dataForm.lotDeviceId,
              'deviceType': this.dataForm.deviceType,
              'deviceKind': this.dataForm.deviceKind,
              'name': this.dataForm.name,
              'userId': this.dataForm.userId,
              'longitude': this.dataForm.longitude,
              'latitude': this.dataForm.latitude,
              'createTime': this.dataForm.createTime,
              'province': this.dataForm.province,
              'city': this.dataForm.city,
              'area': this.dataForm.area,
              'simCode': this.dataForm.simCode
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    userChange (user) {
      console.info(user)
      this.dataForm.userId = user
    },
    searchUserByName (query) {
      if (query !== '') {
        this.loading = true
        this.userForm.username = query
        this.getAppUserList()
      } else {
        this.userPage.userDataList = []
      }
    },
    getAppUserList () {
      this.$http({
        url: this.$http.adornUrl('/user/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'username': this.userForm.username
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.userPage.userDataList = data.page.list
          this.userPage.total = data.page.totalCount
        } else {
          this.userPage.userDataList = []
          this.userPage.total = 0
        }
      })
    }
  }
}
</script>
